import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;

/**
 * @author Dennis Li
 * @date 2020/10/18 21:49
 */
public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10);

        for (int i = 0; i < 5; i++) {
            queue.put(i);
        }

        Thread thread = new Thread(() -> {
            while (queue.remainingCapacity() > 0) ;
            while (queue.size() > 0) {
                try {
                    queue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

        for (int i = 5; i < 11; i++) {
            queue.put(i);
            System.out.println(queue.remainingCapacity());
        }

        thread.join();

        System.out.println("The job is ended");
    }

}
